package com.mdd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.R;
import com.mdd.dto.SetmealDishDto;
import com.mdd.dto.SetmealDto;
import com.mdd.pojo.Setmeal;
import com.mdd.pojo.SetmealDish;
import com.mdd.service.DishService;
import com.mdd.service.SetmealDishService;
import com.mdd.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private DishService dishService;

    /**
     * 获取套餐内的菜品
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<List<SetmealDishDto>> getSetmelAllDish(@PathVariable("id") Long id){
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id).orderByDesc(SetmealDish::getUpdateTime);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        ArrayList<SetmealDishDto> setmealDishDtos = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtils.copyProperties(setmealDish,setmealDishDto);
            Long dishId = setmealDish.getDishId();
            String image = dishService.getById(dishId).getImage();
            setmealDishDto.setImage(image);
            setmealDishDtos.add(setmealDishDto);
        }
        return R.success(setmealDishDtos);
    }

    /**
     * 新增套餐
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> addSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.addSetmeal(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Cacheable(value = "setmealCache",key = "#page+'_'+#pageSize")
    public R<Page<SetmealDto>> pageList(int page ,int pageSize ,String name){
        Page<SetmealDto> setmealDtoPage = setmealService.pageList(page, pageSize, name);
        return R.success(setmealDtoPage);
    }

    /**
     * 批量修改套餐状态或者单个套餐状态
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateSetmealStatus(@PathVariable("status") int status,@RequestParam("ids") List<Long> ids){
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(Setmeal::getId,ids).set(Setmeal::getStatus,status);
        setmealService.update(updateWrapper);
        return R.success("修改状态成功！");
    }

    /**
     * 删除单个套餐或者多个套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> deleteSetmeal(@RequestParam("ids")List<Long> ids){
        setmealService.removeSetmeal(ids);
        return R.success("删除成功");
    }


    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("list")
    public R<List<Setmeal>> setmealList(Setmeal setmeal){
        //获取条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //根据CategoryId查询同一套餐类型的所有在售套餐
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId())
                .eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus())
                .orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmeals = setmealService.list(queryWrapper);

        return R.success(setmeals);

    }

    /**
     * 根据Id获取套餐基本数据以及套餐包含的菜品信息
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealInfo(@PathVariable("id") Long id){
        SetmealDto setmealDto = new SetmealDto();
        //通过id获取该套餐的基本信息
        Setmeal setmeal = setmealService.getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);

        //获取该套餐包含的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> dishes = setmealDishService.list(queryWrapper);

        //将菜品信息封装到dto中
        setmealDto.setSetmealDishes(dishes);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐基本信息以及套餐的菜品信息
     * @return
     */
    @PutMapping
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        //根据id修改套餐基本信息
        setmealService.updateById(setmealDto);
        //删除原来套餐里的所有菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //给新修改的菜品设置套餐Id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }
        //重新添加新修改的所有菜品信息
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }
}
